#ifndef _TVM_UTILITY_H_
#define _TVM_UTILITY_H_

#ifdef __cplusplus
extern "C" 
{
#endif

/*some utility functions and macros*/
#include "tvm_typedef.h"
#include "tvm_limits.h"


#define TVM_BUF_SIZE			  				33/*preserve 3 byte for "\r\n\0"*/ 
#define TVM_DEFAULT_PERIOD		  	50 
#define SIZE_OF_BYTE_IN_BIT			 				8 
#define SIZE_OF_DWORD_IN_BYTE	 				( sizeof (uint32) )
#define SIZE_OF_DWORD_IN_BIT		 				( SIZE_OF_BYTE_IN_BIT * SIZE_OF_DWORD_IN_BYTE )

#define SIZE_OF_SIZE_TYPE_IN_BYTE		( sizeof( size_type ) )
#define SIZE_OF_SIZE_TYPE_IN_BIT		( SIZE_OF_SIZE_TYPE_IN_BYTE * SIZE_OF_BYTE_IN_BIT )

#define ASCII_MIN				 					0 
#define ASCII_MAX				 					SINT8_MAX

#define IS_ASCII(byte)		( (byte) >  ASCII_MIN && (byte) <  ASCII_MAX )

#define	 GET_ITH_BYTE(n, i)			( (n) >> ( (i)*SIZE_OF_BYTE_IN_BIT ) ) & UINT8_MAX 
#define SET_ITH_BYTE(n, i, val)		(n) &=  ~(UINT8_MAX << ( (i)*SIZE_OF_BYTE_IN_BIT ) );\
															(n) |=  ( (val) << ( (i)*SIZE_OF_BYTE_IN_BIT ) ) 

#define GET_ITH_BIT(n, i)		( ( (n) >> (i) ) & 1)
#define SET_ITH_BIT(n, i)		( (n) |=  (1<<(i) ) )
#define CLR_ITH_BIT(n, i)		(n) &=  ~(1<<(i) )

#define MAX(a, b)		 ( (a) > (b) ? (a) : (b) )
#define MIN(a, b)		 ( (a) < (b) ? (a) : (b) )

int8* GetBuffer(void);

size_type StrLen(const int8*str);

int8* StrAppend(int8* buf, size_type bufSize, const int8* str);

int8* StrAppendInt(int8* buf, size_type bufSize, uint32 number);

size_type GetBitRange(size_type dword, size_type from, size_type to, size_type*pRet);

void GetPrefixAndUnit(uint32 time, uint32*pPrefix, const int8**ppUnitStr);

#ifdef __cplusplus
}
#endif

#endif/*_TVM_UTILITY_H_*/
